package com.kitty.game.gm;

import com.kitty.common.schedule.ScheduledServiceNew;
import com.kitty.common.start.GameServer;
import com.kitty.common.utils.AndroidDesUtil;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.ServerService;
import com.kitty.game.admin.service.AdminService;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.service.HDDSService;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.game.party.PartyDataPool;
import com.kitty.game.party.model.Party;
import com.kitty.game.party.service.PartyService;
import com.kitty.game.player.PlayerService;
import com.kitty.game.role.model.Account;
import com.kitty.game.role.model.AccountLogin;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.AccountService;
import com.kitty.game.role.service.PayService;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.model.Member;
import com.kitty.game.team.model.Team;
import com.kitty.game.utils.TimeUtil;
import com.kitty.game.welfare.model.CommonFetchedData;
import com.kitty.game.welfare.service.WelfareService;
import com.kitty.listener.EventDispatcher;
import com.kitty.listener.EventType;
import com.kitty.listener.event.RoleNameChangeEvent;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.mina.core.session.IoSession;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.lang.util.NutMap;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

//后加


@Service
public class GmCommandHandler200 {

    public void kuafu(Role gm,String content){
        String[] strings = content.split("##");
        SpringUtils.getRoleService().wuxing.put(strings[0],content);
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("设置五行成功"));
    }

    public void resetShop(Role gm,String content){
        ArrayList<Byte> arrayList = new ArrayList<>(DataCache.RECHARGE_REWARD_HISTORYS.keySet());
        for (byte bb: arrayList){
            DataCache.RECHARGE_REWARD_HISTORYS.put(bb, (short) 0);
        }
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("重置积分商城成功"));
    }

    public void wuxing(Role gm,String content){
        String[] strings = content.split("##");
        SpringUtils.getRoleService().wuxing.put(strings[0],content);
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("设置五行成功"));
    }
    public void move(Role gm,String content){
        String[] strings= content.split("##");
        Sql sql = Sqls.create("update t_account set channelCode=@channelCode where username=@username");
        sql.setParam("channelCode",strings[1]);
        sql.setParam("username",strings[0]);
        SpringUtils.getBean(AccountService.class).update(sql);
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("更换码子成功"));
    }
    public void moveAll(Role gm,String content){
        String[] strings= content.split("##");
        Sql sql = Sqls.create("update t_account set channelCode=@channelCode where channelCode=@channelCode111");
        sql.setParam("channelCode",strings[1]);
        sql.setParam("channelCode111",strings[0]);
        SpringUtils.getBean(AccountService.class).update(sql);
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("更换码子成功"));
    }
    public void unnomal(Role gm,String content){
        long uid = getUidByName(content.trim());
        IoSession session = SessionUtils.getSessionBy(uid);
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("角色不存在"));
            return;
        }
        Team team = SpringUtils.getTeamService().getTeam(role.getRoleId());
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("队伍信息：#R");
        if (team != null){
            ArrayList<Member> arrayList = new ArrayList<>(team.getList());
            for (Member member: arrayList){
                stringBuilder.append("roleid="+member.getRoleId());
                stringBuilder.append("，");
                stringBuilder.append("name="+member.getName());
                stringBuilder.append("，");
                stringBuilder.append("uid="+member.getUid());
                stringBuilder.append("，");
            }
        }

        stringBuilder.append("#n战斗信息：#R");
        Fight fight = SpringUtils.getFightService().getFightByRoleId(role.getRoleId());
        if (fight != null){
            ArrayList<FightObject> list = new ArrayList(fight.getAllFightObjects());
            for (FightObject fightObject : list){
                if (fightObject.getPet() != null){
                    stringBuilder.append("petId="+fightObject.getPet().getId());
                    stringBuilder.append("，");
                    stringBuilder.append("petName="+fightObject.getPet().getName());
                    stringBuilder.append("，");
                }else if (fightObject.getRole() != null){
                    stringBuilder.append("roleid="+fightObject.getRole().getRoleId());
                    stringBuilder.append("，");
                    stringBuilder.append("name="+fightObject.getRole().getName());
                    stringBuilder.append("，");
                    stringBuilder.append("uid="+fightObject.getRole().getUid());
                    stringBuilder.append("，");
                }else {
                    stringBuilder.append("name="+fightObject.getName());
                    stringBuilder.append("，");
                }
            }
        }
        MessagePusher.pushMessage(role,new RespNotifyMiscEx("异常角色信息："+stringBuilder.toString()));
    }


    public void doubleExp(Role gm,String content){
        CommonFetchedData commonFetchedData = SpringUtils.getBean(HDDSService.class).getExpRate();
        commonFetchedData.setData(content);
        SpringUtils.getBean(Dao.class).insertOrUpdate(commonFetchedData);
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("设置经验成功"));
    }

    public void doubleDaohang(Role gm,String content){
        CommonFetchedData commonFetchedData = SpringUtils.getBean(HDDSService.class).getDaohangRate();
        commonFetchedData.setData(content);
        SpringUtils.getBean(Dao.class).insertOrUpdate(commonFetchedData);
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("设置道行成功"));
    }

    /**
     * 随机抽奖
     * @param gm
     * @param content
     */
    public void randomChoujiang(Role gm,String content){
        SpringUtils.getBean(WelfareService.class).resetRandomActivity(gm,content);
        SpringUtils.getBean(ScheduledServiceNew.class).randomActivity1();
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("随机抽奖充值活动设置成功"));
    }


    public void clearSign(Role gm,String content){
        Dao dao = SpringUtils.getBean(Dao.class);
        String sqlstr = "select uid from t_role";
        Sql sql = Sqls.create(sqlstr);
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
                ArrayList<Long>arrayList = new ArrayList();
                while (resultSet.next()){
                    Long uid = resultSet.getLong("uid");
                    arrayList.add(uid);
                }
                return arrayList;
            }
        });
        dao.execute(sql);
        List<Long> list = sql.getList(Long.class);
        PlayerService playerService = SpringUtils.getPlayerService();
        for (Long integer:list){
            Role aaaaaa = playerService.getPlayerBy(integer);
            if (aaaaaa == null ){
                continue;
            }
            aaaaaa.getExtendBox().setSignDay("");
            aaaaaa.getExtendBox().setSignSize(0);
            aaaaaa.getExtendBox().setSignbuSign("");
            aaaaaa.save();
            MessagePusher.pushMessage(aaaaaa,new RespNotifyMiscEx("签到已经重置！！"));
        }
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("签到已经重置"));
    }

    public void dismissParty(Role gm,String content){
        PartyService partyService = SpringUtils.getPartyService();
        Party party = partyService.getParty(gm.getPartyId());
        if (party == null) {
            MessagePusher.pushMessage(gm,new RespNotifyMiscEx("没有帮派"));
            return ;
        }
        party.getMembers().clear();

        /**添加帮主称号*/
        partyService.addPartyTitle(party, gm);
        SpringUtils.getRoleService().sendExtraAttribute(gm);
        Dao dao = SpringUtils.getBean(Dao.class);
        {
            String sqlStr = "update t_role set partyId=0";
            Sql sql = Sqls.create(sqlStr);
            dao.execute(sql);
        }
        RoleService roleService = SpringUtils.getRoleService();
        Map<Long, IoSession> map = SpringUtils.getBean(GameServer.class).getAcceptor().getManagedSessions();
        for (Map.Entry<Long, IoSession> entry : map.entrySet()) {
            IoSession session = entry.getValue();
            Role tempRole = SessionUtils.getRoleBySession(session);
            if (tempRole == null) {
                continue;
            }
            tempRole.setPartyId(0);

        }
        for (Map.Entry<Long, IoSession> entry : map.entrySet()) {
            IoSession session = entry.getValue();
            Role tempRole = SessionUtils.getRoleBySession(session);
            if (tempRole == null) {
                continue;
            }
            partyService.doLeaveParty(tempRole);
        }
        PartyDataPool.partyMap.clear();
        {
            String sqlStr = "DELETE FROM t_party";
            Sql sql = Sqls.create(sqlStr);
            dao.execute(sql);
        }
//        String job = Const.PARTY_JOB_BANG_ZHU + ":-1";
//        partyService.addPartyMember(party, gm, job);
//
//        PartyDataPool.partyMap.put(party.getId(), party);
//        party.save();
//        PartyDataPool.partyName2Id.put(party.getPartyName(), party.getId());
//        gm.setPartyId(party.getId());
//        gm.save();
    }

    public void reset(Role gm,String content){
        Dao dao = SpringUtils.getBean(Dao.class);
        String sqlstr = "select uid from t_role";
        Sql sql = Sqls.create(sqlstr);
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
                ArrayList<Long>arrayList = new ArrayList();
                while (resultSet.next()){
                    Long uid = resultSet.getLong("uid");
                    arrayList.add(uid);
                }
                return arrayList;
            }
        });
        dao.execute(sql);
        List<Long> list = sql.getList(Long.class);
        PlayerService playerService = SpringUtils.getPlayerService();
        for (Long integer:list){
            Role aaaaaa = playerService.getPlayerBy(integer);
            if (aaaaaa == null ){
                continue;
            }
            SpringUtils.getRoleService().doDailyFiveReset(aaaaaa);
            MessagePusher.pushMessage(aaaaaa,new RespNotifyMiscEx("日常任务已经重置，请重新登录查看！！"));
        }
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("所有玩家任务重置成功"));
    }

    public void delMail(Role gm,String content){
        String name = content;
        name = SpringUtils.getBean(GmService.class).replaceName(name);
        Role role = SpringUtils.getRoleService().getOnlinePlayer(name);
        if (role == null){
            long uid = getUidByName(name);
            role = SpringUtils.getRoleService().getPlayerBy(uid);
            if (role == null){
                MessagePusher.pushMessage(gm,new RespNotifyMiscEx("角色不存在"));
                return;
            }
        }
        role.getMailBox().getAllMail().clear();
        role.save();
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("清空邮件成功"));
    }

    public void pwd(Role gm,String content){
        String[] strings = content.split("##");
        String name=strings[0];
        String pwd = strings[1];
        name = SpringUtils.getBean(GmService.class).replaceName(name);
        Role role = SpringUtils.getRoleService().getOnlinePlayer(name);
        if (role == null){
            long uid = getUidByName(name);
            role = SpringUtils.getRoleService().getPlayerBy(uid);
            if (role == null){
                MessagePusher.pushMessage(gm,new RespNotifyMiscEx("角色不存在"));
                return;
            }
        }
        AccountLogin accountLogin = SpringUtils.getAccountService().queryAccount(role.getSid());
        if (accountLogin == null){
            MessagePusher.pushMessage(gm,new RespNotifyMiscEx("没有账号"));
            return;
        }
        String dd = AndroidDesUtil.encrypt(pwd, "leiting");
        accountLogin.setPwd(dd);
        if (strings.length == 3){
            accountLogin.setSuperPass(strings[2]);
        }
        SpringUtils.getAccountService().updateAccount(accountLogin,"pwd|superPass");
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("修改密码成功！！"));
    }

//    public void delAllRole(Role gm,String content){
//        int level = Integer.parseInt(content);
//        Collection<Role> roles = DataCache.ONLINE_ROLES.values();
//        for (Role role : roles) {
//            if (role.getLevel() <=level){
//                Account account = SpringUtils.getAccountService().getAccount(role.getSid());
//                if (account != null && account.getPrivilege() >100){
//                    continue;
//                }
//                role.setLockTime(System.currentTimeMillis()+6000*TimeUtil.ONE_HOUR);
//                role.save();
//                IoSession session = SessionUtils.getSession(role.getRoleId());
//                if (session != null){
//                    session.closeNow();
//                }
//            }
//        }
//        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("删除成功！！"));
//    }

    public void transRole(Role gm, String content){
        SpringUtils.getPlayerService().transRole(gm, content);
    }

    public void checkAcc(Role gm, String content) {
        content = content.trim().replaceAll(" ","");
        Dao dao = SpringUtils.getBean(Dao.class);
        Account account = dao.fetch(Account.class,Cnd.where("username","=",content));
        if (account == null){
            MessagePusher.pushMessage(gm,new RespNotifyMiscEx("账号不存在"));
            return;
        }
        Sql sql = Sqls.create("select name,roleid from t_role where sid=@sid");
        sql.setParam("sid",account.getId());
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
                ArrayList<NutMap> arrayList = new ArrayList<>();
                while (resultSet.next()){
                    NutMap nutMap = new NutMap();
                    nutMap.put("name",resultSet.getString("name"));
                    nutMap.put("roleid",resultSet.getString("roleid"));
                    arrayList.add(nutMap);
                }
                return arrayList;
            }
        });
        dao.execute(sql);
        List<NutMap> arrayList = sql.getList(NutMap.class);
        for (NutMap nutMap : arrayList){
            MessagePusher.pushMessage(gm,new RespNotifyMiscEx("名字:"+nutMap.getString("name")+" ID:"+nutMap.getInt("roleid")));
        }
    }

    public void name(Role gm, String content) {
        content = content.trim();
        String[] strings = content.split("##");
        Role temp = SpringUtils.getBean(Dao.class).fetch(Role.class, Cnd.where("roleId", "=", Integer.parseInt(strings[0])));
        if (temp == null) {
            return;
        }
        temp.doAfterInit();
        // 内存中是否有这个角色
        Role role = SpringUtils.getPlayerService().getPlayerBy(temp.getUid());
        if (role != null){
            temp = role;
        }
        String oldName = temp.getName();
//        Set<String> nameList = temp.getExtendBox().getNameList();
//        nameList.add(oldName);
        temp.setName(strings[1]);
        temp.save();
        // 让玩家重上
        SpringUtils.getPlayerService().updateRoleName(oldName, temp);
//        SpringUtils.getRoleService().modifyName(temp, strings[1]);
        EventDispatcher.getInstance().fireEvent(new RoleNameChangeEvent(EventType.ROLE_NAME_CHANGE, temp, oldName, strings[1]));

        IoSession session = SessionUtils.getSessionBy(temp.getUid());
        if (session != null){
            session.closeNow();
        }
    }


//    public void delRole(Role gm, String name){
//        name = SpringUtils.getBean(GmService.class).replaceName(name);
//        Role role = SpringUtils.getRoleService().getOnlinePlayer(name);
//        if (role == null){
//            long uid = getUidByName(name);
//            role = SpringUtils.getRoleService().getPlayerBy(uid);
//            if (role == null){
//                MessagePusher.pushMessage(gm,new RespNotifyMiscEx("角色不存在"));
//                return;
//            }
//        }
//        // 先封号
//        role.setLockTime(System.currentTimeMillis()+600*TimeUtil.ONE_HOUR);
//        // 下线
//        IoSession session = SessionUtils.getSession(role.getRoleId());
//        if (session != null){
//            session.closeNow();
//        }
//        SpringUtils.getPlayerService().delProfile(role.getSid(),role.getName());
//        role.setSid(role.getSid()+"_del");
//        role.save();
//        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("删除角色成功！！"));
//    }
    public void jinyan(Role aaa,String content){
        String[] strings = content.split("##");
        String name = strings[0];
        name = SpringUtils.getBean(GmService.class).replaceName(name);
        String boo = strings[1];
        Role role = SpringUtils.getRoleService().getOnlinePlayer(name);
        if (role == null){
            long uid = getUidByName(name);
            role = SpringUtils.getRoleService().getPlayerBy(uid);
            if (role == null){
                MessagePusher.pushMessage(aaa,new RespNotifyMiscEx("角色不存在"));
                return;
            }
        }
        AdminService adminService = SpringUtils.getBean(AdminService.class);
        if (boo.equals("1")){
            adminService.shutChannel(role, System.currentTimeMillis() + 90 * TimeUtil.ONE_DAY);
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("你已经被禁言！"));
        }else {
            adminService.shutChannel(role,  0);
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("你的禁言已经解开，可以发言！"));
            MessagePusher.pushMessage(aaa,new RespNotifyMiscEx("禁言已经解开"));
        }
    }

    public void fenbi(Role aaa,String content){
        String[] strings = content.split("##");
        String name = strings[0];
        name = SpringUtils.getBean(GmService.class).replaceName(name);
        String boo = strings[1];
        Role role = SpringUtils.getRoleService().getOnlinePlayer(name);
        if (role == null){
            long uid = getUidByName(name);
            role = SpringUtils.getRoleService().getPlayerBy(uid);
            if (role == null){
                MessagePusher.pushMessage(aaa,new RespNotifyMiscEx("角色不存在"));
                return;
            }
        }
        long uid = role.getUid();
        AdminService adminService = SpringUtils.getBean(AdminService.class);
        if (boo.equals("1")){
            adminService.blockPlayer(role, System.currentTimeMillis()+90 * TimeUtil.ONE_DAY);
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("你已被封禁!"));
            ServerService serverService = SpringUtils.getBean(ServerService.class);
            serverService.getScheduledExecutorService().schedule(new Runnable() {
                @Override
                public void run() {
                    IoSession session = SessionUtils.getSessionBy(uid);
                    if (session != null){
                        session.closeNow();
                    }
                }
            },3, TimeUnit.SECONDS);
        }else {
            adminService.blockPlayer(role, 0);
            MessagePusher.pushMessage(aaa,new RespNotifyMiscEx("封闭已经解开"));
        }
    }
    public void openAcc(Role gm,String username){
        AccountService accountService = SpringUtils.getBean(AccountService.class);
        AccountLogin accountLogin = accountService.queryByUsername(username);
        if (accountLogin == null){
            MessagePusher.pushMessage(gm,new RespNotifyMiscEx("账号不存在"));
            return;
        }
        accountLogin.setIsStop(0);
        accountService.updateAccount(accountLogin,"isStop");
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("解锁账号成功！！"));
    }

    public void daohangAll(Role aa, int value) {
        Dao dao = SpringUtils.getBean(Dao.class);
        String sqlstr = "select uid from t_role where level >70";
        Sql sql = Sqls.create(sqlstr);
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
                ArrayList<Long>arrayList = new ArrayList();
                while (resultSet.next()){
                    Long uid = resultSet.getLong("uid");
                    arrayList.add(uid);
                }
                return arrayList;
            }
        });
        dao.execute(sql);
        List<Long> list = sql.getList(Long.class);
        PlayerService playerService = SpringUtils.getPlayerService();
        for (Long integer:list){
            Role aaaaaa = playerService.getPlayerBy(integer);
            if (aaaaaa == null ){
                continue;
            }
            if (aaaaaa.getLevel()<10){
                continue;
            }
            MessagePusher.pushMessage(aaaaaa,new RespNotifyMiscEx("已经收到补偿#R"+value+"点道行！！"));
            SpringUtils.getBean(RoleService.class).addTao(aaaaaa, value);
        }
    }

    public void daohangArea(Role aa, String content) {
        Dao dao = SpringUtils.getBean(Dao.class);
        String[] strings = content.split("##");
        String sqlstr = "select uid from t_role where level >= @min and level <= @max";
        Sql sql = Sqls.create(sqlstr);
        sql.setParam("min",Integer.parseInt(strings[0]));
        sql.setParam("max",Integer.parseInt(strings[1]));
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
                ArrayList<Long>arrayList = new ArrayList();
                while (resultSet.next()){
                    Long uid = resultSet.getLong("uid");
                    arrayList.add(uid);
                }
                return arrayList;
            }
        });
        dao.execute(sql);
        int value = Integer.parseInt(strings[2]);
        List<Long> list = sql.getList(Long.class);
        PlayerService playerService = SpringUtils.getPlayerService();
        for (Long integer:list){
            Role aaaaaa = playerService.getPlayerBy(integer);
            if (aaaaaa == null ){
                continue;
            }
            if (aaaaaa.getLevel()<10){
                continue;
            }
            MessagePusher.pushMessage(aaaaaa,new RespNotifyMiscEx("已经收到补偿#R"+value+"点道行！！"));
            SpringUtils.getBean(RoleService.class).addTotalTao11111(aaaaaa, value);
        }
    }


    public void daohangLibao(Role aa, String content) {
        Dao dao = SpringUtils.getBean(Dao.class);
        String[] strings = content.split("##");
        String sqlstr = "select uid from t_role where level >= @min and level <= @max";
        Sql sql = Sqls.create(sqlstr);
        sql.setParam("min",Integer.parseInt(strings[0]));
        sql.setParam("max",Integer.parseInt(strings[1]));
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
                ArrayList<Long>arrayList = new ArrayList();
                while (resultSet.next()){
                    Long uid = resultSet.getLong("uid");
                    arrayList.add(uid);
                }
                return arrayList;
            }
        });
        dao.execute(sql);
        int value = Integer.parseInt(strings[2]);
        List<Long> list = sql.getList(Long.class);
        PlayerService playerService = SpringUtils.getPlayerService();
        for (Long integer:list){
            Role aaaaaa = playerService.getPlayerBy(integer);
            if (aaaaaa == null ){
                continue;
            }
            if (aaaaaa.getLevel()<10){
                continue;
            }
            SpringUtils.getMailService().buchang(aaaaaa, strings[0], Integer.parseInt(strings[2]));
        }
    }

    public void daoju(Role gm, String content){
        String[] strings = content.split("##");
        String name = strings[0];
        name = SpringUtils.getBean(GmService.class).replaceName(name);
        String daoju = strings[1];
        Role role = SpringUtils.getRoleService().getOnlinePlayer(name);
        if (role == null){
            long uid = getUidByName(name);
            role = SpringUtils.getRoleService().getPlayerBy(uid);
            if (role == null){
                MessagePusher.pushMessage(gm,new RespNotifyMiscEx("角色不存在"));
                return;
            }
        }
        int count = Integer.parseInt(strings[2]);
        SpringUtils.getMailService().buchang(role, daoju, count);
    }


    public void daohang(Role gm,String content){
        String[] args = content.split("##");
        String roleName = args[0];
        roleName = SpringUtils.getBean(GmService.class).replaceName(roleName);
        long targetUid = SpringUtils.getPlayerService().getUidBy(roleName);
        Role role = SpringUtils.getPlayerService().getPlayerBy(targetUid);
        if (role == null) {
            MessagePusher.pushMessage(gm, new RespNotifyMiscEx("角色不存在"));
            return;
        }
        SpringUtils.getBean(RoleService.class).addTotalTao11111(role, Integer.parseInt(args[1]));
    }

    public void mirror(Role gm, String name) {
        name = SpringUtils.getBean(GmService.class).replaceName(name);
        Role role = SpringUtils.getRoleService().getOnlinePlayer(name);
        if (role == null){
            long uid = getUidByName(name);
            role = SpringUtils.getRoleService().getPlayerBy(uid);
            if (role == null){
                MessagePusher.pushMessage(gm,new RespNotifyMiscEx("角色不存在"));
                return;
            }
        }
        SpringUtils.getMirrorService().addFightMirror(SpringUtils.getMirrorService().createFightMirror(994,role));
        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("创建npc成功"));
    }

    public void level(Role role, int level) {
        SpringUtils.getBean(GmService.class).level(role,level);
    }

//    public void chongzhi(Role gm, String content) {
//        String[] strings = content.split("##");
//        String name = strings[0];
//        name = SpringUtils.getBean(GmService.class).replaceName(name);
//        Role role = SpringUtils.getRoleService().getOnlinePlayer(name);
//        if (role == null){
//            long uid = getUidByName(name);
//            role = SpringUtils.getRoleService().getPlayerBy(uid);
//            if (role == null){
//                MessagePusher.pushMessage(gm,new RespNotifyMiscEx("角色不存在"));
//                return;
//            }
//        }
//        PayNotifyInfo payNotifyInfo = new PayNotifyInfo();
//        payNotifyInfo.setMoney(Integer.parseInt(strings[1]));
//        payNotifyInfo.setTradeNo("MY-"+ R.UU32());
//        payNotifyInfo.setAccount(role.getSid());
//        payNotifyInfo.setRoleUid(role.getUid());
//        payNotifyInfo.setGold(Integer.parseInt(strings[2]));
//        payNotifyInfo.setCreateTime(new Date());
//        SpringUtils.getBean(OrderService.class).handleOrder(payNotifyInfo);
//        MessagePusher.pushMessage(gm,new RespNotifyMiscEx("发送充值成功！！"));
//    }

    public long getUidByName(String name){
        String sqlstr = "select uid from t_role where name=@name";
        Sql sql = Sqls.create(sqlstr);
        sql.setParam("name",name);
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
                if (resultSet.next()){
                    return resultSet.getLong("uid");
                }
                return 0;
            }
        });
        SpringUtils.getBean(Dao.class).execute(sql);
        long uid = sql.getLong();
        return uid;
    }

    public void choujiang(Role gm, String content) {
        String[] args = content.split("##");
        String roleName = args[0];
        roleName = SpringUtils.getBean(GmService.class).replaceName(roleName);
        long targetUid = SpringUtils.getPlayerService().getUidBy(roleName);
        Role role = SpringUtils.getPlayerService().getPlayerBy(targetUid);
        if (role == null) {
            MessagePusher.pushMessage(gm, new RespNotifyMiscEx("角色不存在"));
            return;
        }
        SpringUtils.getBean(PayService.class).choujiang(role, Integer.parseInt(args[1]));
    }


}
